<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>react</title>
</head>
<body>
    <div id="app"></div>
</body>
</html>
<script crossorigin src="https://unpkg.com/react@16/umd/react.development.js"></script>
<script crossorigin src="https://unpkg.com/react-dom@16/umd/react-dom.development.js"></script>
<script crossorigin src="https://cdnjs.cloudflare.com/ajax/libs/babel-core/5.8.23/browser.min.js"></script>
<script type="text/babel">

    class List extends React.Component{
        constructor(props){
            super(props);
            this.state={
                data:[],
                addTxt:""
            };
            this.changeTxt=this.changeTxt.bind(this)
            this.addItem=this.addItem.bind(this)
            this.delItem=this.delItem.bind(this)
        }

        changeTxt(e){
            
            this.setState({
                addTxt:e.target.value
            })
        }

        addItem(){
            this.setState({
                //"data":this.state.data.push(this.state.addTxt)
                data:[...this.state.data,this.state.addTxt]
            });
        }

        delItem(index){
            this.setState({
                //"data":this.state.data.push(this.state.addTxt)
                data:[...this.state.data.slice(0,index),...this.state.data.slice(index+1)]
            });
        }

        render() {
            //return null;
            let lis=this.state.data.map((item,index)=>{
                return (
                    <li key={index}>
                        {item}
                        <button onClick={this.delItem.bind(this,index)}>删除</button>
                    </li>
                )
            });
            return (
                <div>
                    <div>
                        <input type="text" value={this.state.addTxt} onChange={this.changeTxt} />
                        <button onClick={this.addItem}>添加</button>
                    </div>
                    <ul>
                        {lis}
                    </ul>
                </div>
            )
        }
    }
        
    

    let App=(props)=>{
        return (
            <div>
                <List />
            </div>
        )
    }
    
    ReactDOM.render(
       <App />,
        document.getElementById('app')
    )
</script>